package org.jsola.hr.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.entity.EmpNoticeDO;
import org.jsola.hr.entity.TaskDO;
import org.jsola.hr.service.IEmpNoticeService;
import org.jsola.hr.service.IEmpNoticeTaskService;
import org.jsola.hr.service.ITaskService;
import org.jsola.user.core.TokenUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;


/**
 * @author JanusMix
 */
@Service("hrEmpNoticeTaskServiceImpl")
@Slf4j
public class EmpNoticeTaskServiceImpl implements IEmpNoticeTaskService {

    @Autowired
    private ITaskService taskService;

    @Autowired
    private IEmpNoticeService empNoticeService;


    @Override
    public void runTask(String taskId) {
        TaskDO task = taskService.selectDOById(taskId, "");
        if (Objects.nonNull(taskId)) {
            log.info("开始执行定时任务-----------:{}", task);
            EmpNoticeDO empNoticeDO = empNoticeService.selectDOById(taskId, "");
            // 拼装当前用户
            TokenUser tokenUser = new TokenUser();
            tokenUser.setSiteId(task.getSiteId());
            tokenUser.setUserId("system");
            if (Objects.nonNull(empNoticeDO)) {
                if (HrConstants.SYS_NOTICE_ONLINE.equals(task.getRunType())) {
                    // 上线
                    empNoticeService.runOnline(task.getSubjectId(), tokenUser);
                } else if (HrConstants.SYS_NOTICE_OFFLINE.equals(task.getRunType())) {
                    // 下线
                    empNoticeService.runOffline(task.getSubjectId(), tokenUser);
                }
            }
            // 任务置为已完成
            taskService.taskRunEnd(task.getId(), tokenUser);
            log.info("定时任务task:{}执行结束", task.getId());
        }
    }
}
